import { getMyBooks, on_login, changeMyBook } from "../../utils/api.js";
const App = getApp();
Page({

    /**
     * 页面的初始数据
     */
    data: {
        path: 'https://img.yuncoding.cn/souti/book',
        myBooks: null,
        isedit: false,
        showSelect: true,
        deleteList: [],
        // login: false,
        // loginStatus: true
    },
    toggleedit: function () {
        this.setData({
            isedit: !this.data.isedit,
            showSelect: !this.data.showSelect
        });
    },
    changeDel: function (e) {
        if (e.detail.action) {
            var list = this.data.deleteList;
            list.push(e.detail.bookid)
        } else {
            var list = this.data.deleteList;
            list.splice(list.indexOf(e.detail.bookid), 1);
        }
    },
    remove: function () {
        // var deleteList = Array.from(this.data.deleteList);
        // console.log(typeof deleteList)
        changeMyBook(this.data.deleteList, 1, (res) => {
            wx.showToast({
                title: '移出成功',
                success: "success"
            });
            this.data.deleteList.forEach((item) => {
                var temp = wx.getStorageSync("bookJoined") ? wx.getStorageSync("bookJoined") : [];
                temp.splice(temp.indexOf(item), 1);
                console.log("已加入" + item);
                wx.setStorageSync("bookJoined", temp)
            });
            this.getData();
        })
    },
    getData: function () {
        getMyBooks().then((data) => {
            this.setData({
                myBooks: data
            });
        });

    },
    onGotUserInfo: function () {
        new Promise((resolve, reject) => {
            this.onLoad();
            if (App.globalData.token) {
                resolve()
            }
        }).then(() => {
            this.getData()
        })
    },
    jump: function () {
        wx.switchTab({
            url: '/pages/search/search',
        })
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        wx.hideTabBar({
            aniamtion: true
        })
        wx.login({
            success: res => {
                // 发送 res.code 到后台换取 openId, sessionKey, unionId
                App.globalData.code = res.code;
                // 获取用户信息
                wx.getSetting({
                    success: res => {
                        if (!res.authSetting['scope.userInfo']) {
                            wx.hideTabBar({
                                aniamtion: true
                            })
                            this.setData({
                                login: false,
                                loginStatus: true
                            })
                        }
                        else if (res.authSetting['scope.userInfo']) {
                            // 已经授权，可以直接调用 getUserInfo 获取头像昵称，不会弹框
                            wx.getUserInfo({
                                withCredentials: true,
                                success: res => {
                                    // 可以将 res 发送给后台解码出 unionId
                                    App.globalData.userInfo = res;
                                    on_login(App.globalData.code, res.encryptedData, res.iv).then((token) => {
                                        App.globalData.token = token;
                                        return getMyBooks();
                                    }).then((data) => {
                                        wx.showTabBar({
                                            aniamtion: true
                                        })
                                        this.setData({
                                            myBooks: data,
                                            login: true,
                                            loginStatus: false
                                        });
                                        data.forEach((item) => {
                                            var temp = wx.getStorageSync("bookJoined") ? wx.getStorageSync("bookJoined") : [];
                                            if (temp.indexOf(item.id) == -1) {
                                                temp.push(item.id);
                                                console.log("已加入" + item.id);
                                            }
                                            wx.setStorageSync("bookJoined", temp)
                                        })
                                    })
                                    // 由于 getUserInfo 是网络请求，可能会在 Page.onLoad 之后才返回
                                    // 所以此处加入 callback 以防止这种情况
                                    if (App.userInfoReadyCallback) {
                                        App.userInfoReadyCallback(res)
                                    }
                                }
                            })
                        }
                    }
                })
            }
        })
    },
    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
        if (App.globalData.token) {
            this.getData();
        }
    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {
        wx.showNavigationBarLoading();
        if (!App.globalData.token) {
            // 隐藏导航栏加载框
            wx.hideNavigationBarLoading();
            // 停止下拉动作
            wx.stopPullDownRefresh();
            return;
        }
        getMyBooks().then((data) => {
            this.setData({
                myBooks: data
            });
            // 隐藏导航栏加载框
            wx.hideNavigationBarLoading();
            // 停止下拉动作
            wx.stopPullDownRefresh();
        })
    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})